package com.yanqu.road.server.gameplayer.module.cricket.model;

import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.manger.config.SkillMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CricketBattleData {

    private static Logger logger = LogManager.getLogger(CricketBattleData.class.getName());

    public static CricketBattleData getInstance(CricketProto.CricketMatchDataTemp cricketMatchDataTemp) {
        return new CricketBattleData(cricketMatchDataTemp);
    }

    public CricketBattleData(CricketProto.CricketMatchDataTemp cricketMatchDataTemp) {
        this.cricketId = cricketMatchDataTemp.getCricketId();
        this.userId = cricketMatchDataTemp.getUserId();
        this.property = cricketMatchDataTemp.getProperty();
        this.cricketMatchDataTemp = cricketMatchDataTemp;
        for (CricketProto.CricketSkillTempMsg cricketSkillTempMsg : cricketMatchDataTemp.getBattleSkillList()) {
            SkillInfo skillInfo = SkillMgr.getSkillInfo(cricketSkillTempMsg.getSkillId());
            if (skillInfo == null) {
                logger.error("getSkillInfo null, skillId: {}", cricketSkillTempMsg.getSkillId());
                continue;
            }

            Map<Integer, List<SkillEffect>> listMap = new HashMap<>();

            try {
                SkillEffect skillEffect = SkillEffect.getInstance(skillInfo);
                if (skillEffect.getSkillType() == eSkillType.CricketBattleSkill138.getValue()) {
                    continue;
                }
                if (skillEffect.getSkillType() == eSkillType.CricketBattleSkill139.getValue()) {
                    continue;
                }
                if (skillEffect.getSkillType() == eSkillType.CricketBattleSkill140.getValue()) {
                    continue;
                }

                //这些技能，直接叠加上去
                if (skillEffect.getSkillType() == eSkillType.CricketBattleSkill145.getValue() ||
                        skillEffect.getSkillType() == eSkillType.CricketBattleSkill146.getValue() ||
                        skillEffect.getSkillType() == eSkillType.CricketBattleSkill147.getValue()) {
                    SkillEffect skillEffectByType = this.getSkillEffectByType(skillEffect.getSkillType());
                    if (skillEffectByType == null) {
                        this.skillEffectList.add(skillEffect);
                        continue;
                    }
                    skillEffectByType.setP1(skillEffectByType.getP1() + skillEffect.getP1());

                } else {
                    this.skillEffectList.add(skillEffect);
                }

                //todo 调试代码
//                if (skillEffect.getSkillType() != eSkillType.CricketBattleSkill137.getValue()) {
//                    continue;
//                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //todo  这里增加一下技能，方便测试

//        List<Integer> list = new ArrayList<>();
//        list.add(634034);
//        list.add(634044);
//        list.add(634054);
//        list.add(634064);
//        list.add(634074);
//        list.add(634084);
//        list.add(634094);
//        list.add(634104);
//        list.add(634114);
//        list.add(634124);
//        list.add(634130);
//        list.add(634140);
//        list.add(634150);
//        list.add(634160);
//        list.add(634170); //免疫怯场
//        list.add(634180);
//        list.add(634190);
//        list.add(634200);
//        list.add(634210);
//        list.add(634220);
//        for (int skillId : list) {
//            SkillInfo skillInfo = SkillMgr.getSkillInfo(skillId);
//            SkillEffect skillEffect = SkillEffect.getInstance(skillInfo);
//            this.skillEffectList.add(skillEffect);
//        }

    }

    private long userId;
    private int cricketId;
    private List<SkillEffect> skillEffectList = new ArrayList<>();

    private CricketProto.CricketPropertyTempMsg property;


    private CricketProto.CricketMatchDataTemp cricketMatchDataTemp;

    public int getCricketId() {
        return cricketId;
    }

    public List<SkillEffect> getSkillEffectList() {
        return skillEffectList;
    }

    public CricketProto.CricketPropertyTempMsg getProperty() {
        return property;
    }

    public CricketProto.CricketMatchDataTemp getCricketMatchDataTemp() {
        return cricketMatchDataTemp;
    }

    public long getUserId() {
        return userId;
    }


    public SkillEffect getSkillEffectByType(int type) {
        for (SkillEffect skillEffect : this.skillEffectList) {
            if (skillEffect.getSkillType() == type) {
                return skillEffect;
            }
        }
        return null;
    }


}
